package com.aaa.car.service.impl;

import com.aaa.car.entity.Car;
import com.aaa.car.mapper.CarMapper;
import com.aaa.car.service.CarService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.entity.DictData;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Car)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-19 19:32:32
 */
@Service("carService")
public class CarServiceImpl implements CarService {
    @Resource
    private CarMapper carMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Car queryById(Integer id) {
        return this.carMapper.queryById(id);
    }

    /**
     * 查询
     *
     * @param
     * @param
     * @return 对象列表
     */
    @Override
    public Result queryAllCar(Integer pageNo, Integer pageSize, Map map) {
        if (pageNo == null || pageSize == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Map> pageInfo = new PageInfo<>(carMapper.queryAllCar(map));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param  map 实例对象
     * @return 对象列表
     */
    @Override
    public List<Map> queryAll(Map map) {
        return this.carMapper.queryAllCar(map);
    }

    /**
     * 新增数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @Override
    public Car insert(Car car) {
        this.carMapper.insert(car);
        return car;
    }

    /**
     * 修改数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @Override
    public Car update(Car car) {
        this.carMapper.update(car);
        return this.queryById(car.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.carMapper.deleteById(id) > 0;
    }
}